Add manual bindings for MutableTree reading
authorColin Walters <walters@verbum.org>
Thu, 17 Feb 2022 23:36:43 +0000 (18:36 -0500)
committerColin Walters <walters@verbum.org>
Fri, 6 May 2022 16:53:57 +0000 (12:53 -0400)
I'm trying to debug a problem in ostree-rs-ext, and it's
handy to be able to do `dbg!(mtree.copy_files())`.

rust-bindings/rust/src/lib.rs
rust-bindings/rust/src/mutable_tree.rs [new file with mode: 0644]
rust-bindings/rust/tests/util/mod.rs

index fece520656eb87df974a879e83ad68121a8f4420..1cbeabd761b74112972d917a06f934309db0309c 100644 (file)
@@ -47,6 +47,8 @@ mod collection_ref;
 pub use crate::collection_ref::*;
 mod functions;
 pub use crate::functions::*;
+mod mutable_tree;
+pub use crate::mutable_tree::*;
 #[cfg(any(feature = "v2019_3", feature = "dox"))]
 mod kernel_args;
 #[cfg(any(feature = "v2019_3", feature = "dox"))]
diff --git a/rust-bindings/rust/src/mutable_tree.rs b/rust-bindings/rust/src/mutable_tree.rs
new file mode 100644 (file)
index 0000000..dd59217
--- /dev/null
@@ -0,0 +1,46 @@
+use crate::MutableTree;
+use glib::{self, translate::*};
+use std::collections::HashMap;
+
+impl MutableTree {
+    #[doc(alias = "ostree_mutable_tree_get_files")]
+    /// Create a copy of the files in this mutable tree.
+    /// Unlike the C version of this function, a copy is made because providing
+    /// read-write access would introduce the potential for use-after-free bugs.
+    pub fn copy_files(&self) -> HashMap<String, String> {
+        unsafe {
+            let v = ffi::ostree_mutable_tree_get_files(self.to_glib_none().0);
+            HashMap::from_glib_none_num(v, 1)
+        }
+    }
+
+    #[doc(alias = "ostree_mutable_tree_get_subdirs")]
+    /// Create a copy of the directories in this mutable tree.
+    /// Unlike the C version of this function, a copy is made because providing
+    /// read-write access would introduce the potential for use-after-free bugs.
+    pub fn copy_subdirs(&self) -> HashMap<String, Self> {
+        use glib::ffi::gpointer;
+
+        unsafe {
+            let v = ffi::ostree_mutable_tree_get_subdirs(self.to_glib_none().0);
+            unsafe extern "C" fn visit_hash_table(
+                key: gpointer,
+                value: gpointer,
+                hash_map: gpointer,
+            ) {
+                let key: String = from_glib_none(key as *const libc::c_char);
+                let value: MutableTree = from_glib_none(value as *const ffi::OstreeMutableTree);
+                let hash_map: &mut HashMap<String, MutableTree> =
+                    &mut *(hash_map as *mut HashMap<String, MutableTree>);
+                hash_map.insert(key, value);
+            }
+            let mut map = HashMap::with_capacity(glib::ffi::g_hash_table_size(v) as usize);
+            glib::ffi::g_hash_table_foreach(
+                v,
+                Some(visit_hash_table),
+                &mut map as *mut HashMap<String, MutableTree> as *mut _,
+            );
+            map
+        }
+    }
+}
index 2bc4efbf78ee79e5aa1e4ff88b9684ab9794394d..472bf4553e2862e087c290d019fb13dab0de2411 100644 (file)
@@ -50,6 +50,8 @@ impl CapTestRepo {
 
 pub fn create_mtree(repo: &ostree::Repo) -> ostree::MutableTree {
     let mtree = ostree::MutableTree::new();
+    assert_eq!(mtree.copy_files().len(), 0);
+    assert_eq!(mtree.copy_subdirs().len(), 0);
     let file = gio::File::for_path(
         Path::new(env!("CARGO_MANIFEST_DIR"))
             .join("tests")